<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

if ( ! defined( 'IN_ACP' ) )
{
	die("<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.");
}

class ad_ineo
{
	/**
	* Variable Setup
	*/
	var $ipsclass  = null;
	var $html      = null;
	var $ineo      = null;
	
	/**
	* Auto-Run : Called by IPB
	*/
	function auto_run()
	{
		//die( '<pre>'.print_r($this->ipsclass->vars,true) );
		// Load Ineo core.
		require_once( ROOT_PATH . 'sources/ineo/ineo.php' );
		$this->base_url = $this->ipsclass->base_url;
		$this->root_path = ROOT_PATH . 'sources/ineo/';
		$this->ineo = new ineo( $this );
		
		// Since IPB doesn't use our components_init file in the ACP, we need to load
		// our own caches, manually.
		$this->load_caches();
		
		// Load the categories library file, 'cause we need it.
		$this->ineo->load_library( 'categories' );
		
		// Set up some styling information, for easy changing:
		$this->cbox   = 'tableborder';
		$this->ctitle = 'tableheaderalt';
		$this->row1   = 'tablerow1';
		$this->row2   = 'tablerow2';
		$this->thead  = 'tablesubheader';
		$this->tfoot  = 'tablefooter';
		
		// Get the requested module:
		if( !empty($this->ipsclass->input['module']) )
		{
			$module = 'acp_'.$this->ipsclass->input['module'];
		}
		else
		{
			$module = 'acp_overview';
		}
		
		$this->ineo->load_module($module);
		
		// Figure out what we're doing and call it:
		if( !empty( $this->ipsclass->input['code'] ) )
		{
			$action = $this->ipsclass->input['code'];
			$action = preg_replace( '/([^a-zA-Z0-9_]+)/', '', $action );
		}
		else
		{
			$action = 'module_default';
		}
		
		// Try and run the action:
		if( method_exists( $this->ineo->active_module, $action ) )
		{
			$this->ineo->active_module->$action();
		}
		else
		{
			$this->ineo->active_module->module_default();
		}
		
		// Send output:
	   	$this->ipsclass->admin->output();
	}
	
	/**
	* Load Caches
	*/
	function load_caches()
	{
		// Load the caches from the database:
		$cache_load = array('select'=>'*','from'=>'cache_store','where'=>'cs_key IN ( \'ineo_cats\',\'ineo_permissions\' )');
		
		$this->ipsclass->DB->simple_construct($cache_load);
		$this->ipsclass->DB->simple_exec();
		
		// Loop through, unserialize and add each individual cache to the main cache array
		// in IPSClass
		while( $cache = $this->ipsclass->DB->fetch_row() )
		{
			if( $cache['cs_array'] == '1' )
			{
				$_cache = unserialize( $cache['cs_value'] );
			}
			else
			{
				$_cache = $cache['cs_value'];
			}
			
			$newcache = array($cache['cs_key'] => $_cache);
			$this->ipsclass->cache = array_merge($this->ipsclass->cache,$newcache);
		}
	}
	
	/**
	* Quick function for creating an IPB ACP 'box' ( borderwrap, maintitle, content ):
	*/
	function box( $title, $content )
	{
		$this->ipsclass->html .= "
			<div class='{$this->cbox}'>
			
				<div class='{$this->ctitle}'>
					{$title}
				</div>
				
				{$content}
				
			</div>
			<br />
		";
	}
	
	// Builds the dropdown for category listing.
	function build_dropdown($cid='', $no_root = false)
	{
	  $this->ipsclass->DB->simple_construct( array(
	                   'select' => 'category_id, category_name, category_parent',
	                   'from' => 'ineo_categories' ) );
    $this->ipsclass->DB->simple_exec();
    
    $this->depth_guide = '--';
	

    while( $row = $this->ipsclass->DB->fetch_row() )
    {
      $array[$row['category_parent']][$row['category_id']] = $row;
    }
    
    //echo "<pre>";print_r($array);die();
    
    $menu = '<select name="category_parent">';
	
	if( !$no_root )
	{
		$sel = ($this->ipsclass->input['parent'] == 0) ? ' selected="selected"' : '';

		$menu .= "<option value='0'{$sel}>Root Category</option>";
	}
	
    $menu .= $this->build_jump_internal( '0', $array, $cid );
    $menu .= '</select>';
    
    return $menu;
	}
	
	// Recursivityityity!!
  function build_jump_internal($root_id, $array, $selected_id, $jump_string="", $depth_guide="" )
  {
	
    if ( is_array( $array[ $root_id ] ) )
    {
      foreach( $array[ $root_id ] as $id => $cat_data )
      {
      $selected = "";
      if ( $selected_id == $cat_data['category_id'] )
      {
        $selected = ' selected="selected"';
      }
      
      $prefix = $depth_guide == '' ? '' : '&nbsp;&nbsp;&#0124;' ;
      
      $jump_string .= "<option value=\"{$cat_data['category_id']}\"".$selected.">".$prefix.$depth_guide." ".$cat_data['category_name']."</option>\n";
      $jump_string = $this->build_jump_internal( $cat_data['category_id'], $array, $selected_id, $jump_string, $depth_guide . $this->depth_guide );
      }
    }
	  return $jump_string;
  }
}